﻿
using System.Collections.Generic;
using System.Data;
using Core.Library;

namespace DataObject.QA
{
    public class QAController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static int QA_Create(QAObject o)
        {
            return QADAL.QA_Create(o);
        }

        public static void QA_Update(QAObject o)
        {
            QADAL.QA_Update(o);
        }

        public static void QA_Delete(int id)
        {
            QADAL.QA_Delete(id);
        }

        public static void QA_Answer_DeleteAll(int id)
        {
            QADAL.QA_Answer_DeleteAll(id);
        }

        public static void Question_Update_Status(int id, bool isActive)
        {
            QADAL.Question_Update_Status(id, isActive);
        }

        public static QAObject QA_GetById(int id)
        {
            return ObjectHelper.MapRecord<QAObject>(QADAL.QA_GetById(id));
        }

        public static List<QAObject> Question_GetListByTypeByStatusPaging(bool isActive, int pageIndex, int pageSize)
        {
            return ObjectHelper.MapRecords<QAObject>(QADAL.Question_GetListByTypeByStatusPaging(ConvertUtility.BooleanToInt(isActive), pageIndex, pageSize));
        }

        public static int Question_GetStatistic(bool isActive)
        {
            return QADAL.Question_GetStatistic(ConvertUtility.BooleanToInt(isActive));
        }

        public static List<QAObject> Answer_GetListByReplyTo(int id)
        {
            return ObjectHelper.MapRecords<QAObject>(QADAL.Answer_GetListByReplyTo(id));
        }

        /***************************************
        = Front End
        -------------------------------------- */

        internal static DataTable QA_GetQAHomepage(int top)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.QA_GetQAHomepage(top, true, true);
            }
            return dt;
        }

        internal static DataTable QA_GetListPaging(bool isFeature, int pageIndex, int pageSize)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.QA_GetListPaging(true, isFeature, pageIndex, pageSize);
            }
            return dt;
        }

        internal static int QA_Count(bool isFeature)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.QA_Count(true, isFeature);
            }

            if (dt != null && dt.Rows.Count > 0)
                return ConvertUtility.ConvertTo<int>(dt.Rows[0][0]);
            return 0;
        }
    }
}
